<template>
<div class="login">
  <div class="loginForm">
    <h1 class="tit">VueEasyAdmin</h1>
    <p class="desc">集成 <span>vue3 + element plus + vite</span> 快速开发的管理系统模板</p>
    <!-- <p class="desc">VueEasyAdmin是一套可视化拖拽组件快速开发的前端低码管理系统</p> -->
    <el-form
      class="elForm"
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      size="large"
      label-width="auto"
    >
      <el-form-item prop="username">
        <el-input 
          placeholder="用户名..."
          v-model="ruleForm.username" 
          type="text" 
          :prefix-icon="User"
        >
        
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          placeholder="密码..."
          v-model="ruleForm.password"
          type="password"
          :prefix-icon="Lock"
        />
      </el-form-item>

      <el-form-item>
        <el-button 
          class="btn"
          type="primary" 
          @click="submitForm"
        >登录</el-button>
      </el-form-item>

    </el-form>
  </div>
</div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { User,Lock } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';

const Router = useRouter();
const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
  username: 'admin',
  password: '123456',
})

const rules = reactive({
  username: [{ required:true, trigger: 'blur', message:"请输入用户名" }],
  password: [{ required:true, trigger: 'blur', message:"请输入密码"  }],
})

const submitForm = () => {
  if (!ruleFormRef.value) return
  ruleFormRef.value.validate((valid) => {
    if (!valid) {
      console.log('error submit!')
      return false
    } 
      if(ruleForm.username === "admin" && ruleForm.password === "123456") {
        // console.log('submit success!')
        ElMessage.success("登录成功")
        setTimeout(()=>{
          Router.push('/')
        },500)
      }else{
        ElMessage.error("用户名或密码错误")
      }

  })
}

</script>
<style src="./login.less" lang="less" scoped>
</style>
